<?php

/**
 * 通行证用户数据库修改
 *
 * @author 周剑锋<camfee@foxmail.com>
 */

require_once dirname(__DIR__) . "/cronBase.php";

use Common\Bridge;
use lib\core\Action;

class passport extends Action
{
    public function doDefault()
    {
        static $pdo;
        $create_sql = <<<SQL
CREATE TABLE `User` (
  `UserId` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `Email` varchar(128) DEFAULT NULL COMMENT '用户邮箱(唯一)',
  `Mobile` varchar(16) DEFAULT NULL COMMENT '用户手机(唯一)',
  `UserName` varchar(32) DEFAULT NULL COMMENT '登录用户名(唯一)',
  `ModifyName` int(11) NOT NULL DEFAULT '0' COMMENT '用户名\r\n0: 未修改\r\n>1, 次数',
  `Password` varchar(255) DEFAULT NULL COMMENT '新密码Hash',
  `PwdStrong` tinyint(4) NOT NULL DEFAULT '0' COMMENT '密码强度:\r\n0-4: 由弱到强',
  `RegTime` datetime DEFAULT NULL COMMENT '注册时间',
  `RegIp` varchar(64) DEFAULT NULL COMMENT '注册IP(IPV6预留)',
  `LoginTime` datetime DEFAULT NULL COMMENT '最后登录时间',
  `LoginIp` varchar(64) DEFAULT NULL COMMENT '最后登录IP',
  `LoginCount` int(11) NOT NULL DEFAULT '0' COMMENT '登录次数',
  `Status` tinyint(4) NOT NULL DEFAULT '1' COMMENT '用户状态:\r\n0: 帐号锁定\r\n1: 帐号正常',
  `FromPlatform` tinyint(4) NOT NULL DEFAULT '0' COMMENT '注册平台:\r\n0. Web\r\n1. Android-Phone\r\n2. iOS-Phone\r\n3. Mobile Web\r\n4. Win Phone\r\n5. Android-Pad\r\n6. iOS-Pad',
  `FromProduct` tinyint(4) NOT NULL DEFAULT '0' COMMENT '注册产品来源\r\n0: 通行证',
  `FromWay` tinyint(4) NOT NULL DEFAULT '0' COMMENT '注册方式\r\n0: 邮箱 (默认)\r\n1: 手机\r\n20. sina weibo\r\n21. qq weibo\r\n22. qq\r\n23. douban\r\n24. 163\r\n25. taobao\r\n26. weixin',
  `EmailAct` tinyint(4) NOT NULL DEFAULT '0' COMMENT '邮件激活状态:\r\n0: 未激活\r\n1: 已激活',
  `JCoin` int(11) NOT NULL DEFAULT '0' COMMENT '金果',
  PRIMARY KEY (`UserId`),
  UNIQUE KEY `User_UserName` (`UserName`) USING BTREE,
  KEY `User_Email` (`Email`) USING BTREE,
  KEY `User_Mobile` (`Mobile`) USING BTREE,
  KEY `User_Status` (`Status`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT COMMENT='通行证系统1.0';
SQL;
        //$sqls = '';
        $pdo = Bridge::pdo(Bridge::DB_PASSPORT_W);
        $pdo->exec($create_sql);
        for ($i = 0; $i < 256; $i++) {
            $suff = sprintf('%02x', $i);
            $sql = str_replace('`User`', 'User_' . $suff, $create_sql);
            if (empty($pdo)) {
                $pdo = Bridge::pdo(Bridge::DB_PASSPORT_W);
            }
            $pdo->exec($sql);
            //$sqls .= $sql . PHP_EOL;
            echo $i . PHP_EOL;
        }
        //file_put_contents(__DIR__ . '/user.sql', $sqls);
        exit('finished');
    }
}

$app->run();
